<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/27
 * Time: 11:38
 */

namespace app\index\controller;


use Redis\Redis;
use think\Controller;
use think\Db;
use think\Request;

class Service extends Common
{

    private $classIdFilter;
    private $serviceTitleFilter;

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @content 服务列表(添加服务时使用)
     */
    function serviceList()
    {
        #接收服务名称
        $title = input('post.title');
        # 接收服务分类id
        $classId = input('post.classId');
        # 接收车辆等级
        $carLevel = input('post.car_level') ?? 0;
        # 接收用户id
        $memberId = input('post.member_id') ?? 0;
        # 接收车牌号
        $car_num = input('post.car_num');
        if ($car_num == '辽AABCDE') {
            $biz_where = " id in (select sb.service_id from service_biz sb where sb.biz_id = " . $this->bizId . ")";
        } else {
            $biz_where = " id in (select sb.service_id from service_biz sb where sb.biz_id = " . $this->bizId . ") and service_status = 1";
        }
        # 服务/商品筛选
        $add_type = input('post.add_type') ?? 0;
        if (!empty($add_type)) {
            $biz_where .= " and s.add_type = " . $add_type;
        }
        # 判断门店类型
        $bizInfo = Db::table('biz')->field('biz_type,open_custom,extract_selfservice,extract_selfpro,recommend_status')->where(array('id' => $this->bizId))->find();
        $bizType = $bizInfo['biz_type'];
        if ($bizType == 1) {
            $this->bizId = 0;
            $biz_where = '';
        }
        if (!empty($memberId)) {
            # 判断用户状态   会员->线上价格 * 会员折扣/注册->线上价格/未注册=门店价格
            $memberInfo = Db::table('member_mapping')->field('member_level_id,member_code,member_expiration')->where(array('member_id' => $memberId))->find();
            if ($memberInfo['member_level_id'] > 0) {
                # 是会员, or $memberInfo['member_level_id'] == 2
                if (($memberInfo['member_level_id'] == 1) and $memberInfo['member_expiration'] < date('Y-m-d')) {
                    if (!empty($memberInfo['member_code'])) {
                        $level = 'code'; # 已注册
                        $service_type = 1; # 线上价格
                    } else {
                        $level = 'noCode'; # 未注册
                        $service_type = 2; # 门店价格
                    }
                } else {
                    $level = 'member'; # 会员
                    $service_type = 1;
                }
            } else {
                if (!empty($memberInfo['member_code'])) {
                    $level = 'code';
                    $service_type = 1;
                } else {
                    $level = 'noCode';
                    $service_type = 2;
                }
            }
            # 查询服务对应的价格
            $list = Db::table('service s')
                ->field('id,service_title title,service_class_id classId,service_keyborder,is_discount,discount_type,
                if(' . $carLevel . '>0,
                (select discount from service_car_mediscount where service_id=s.id and car_level_id=' . $carLevel . ' and service_type=' . $service_type . ' and biz_id=' . $this->bizId . '),
                (select if(min(discount)=max(discount),(min(discount)),(CONCAT_WS("-",min(discount),max(discount)))) from service_car_mediscount 
                where service_id=s.id and service_type=' . $service_type . ' and biz_id=' . $this->bizId . ')) service_price,
                if(' . $carLevel . '>0,
                (select discount from service_car_mediscount where service_id=s.id and car_level_id=' . $carLevel . ' and service_type=' . $service_type . ' and biz_id=' . $this->bizId . '),
                (select if(min(discount)=max(discount),(min(discount)),(CONCAT_WS("-",min(discount),max(discount)))) from service_car_mediscount 
                where service_id=s.id and service_type=' . $service_type . ' and biz_id=' . $this->bizId . ')) show_price')
                ->where($biz_where)
                ->where(array('is_del' => 2, 'audit_status' => 2, 'service_status' => 1))
                ->select();
            if ($level == 'member') {
                foreach ($list as $k => $v) {
                    # 查询当前服务是否与当前会员绑定
                    $relevance_service = Db::table('relevance_service rs')
                        ->field('discount,car_impose')
                        ->where(array('rs.service_id' => $v['id']))
                        ->where("rs.privilege_id in 
                            (select p.id from privilege p where p.privilege_relevance=1 and find_in_set(p.id,(select level_privilege from level_content lc 
                            where lc.level_id = " . $memberInfo['member_level_id'] . ")))")
                        ->find();
                    if (!empty($relevance_service)) {
                        if (!empty($orderinfo['member_id'])) {
                            # 查询当前车辆是否在限制内
                            if ($relevance_service['car_impose'] > 0) {
                                $car_in_impose = Db::query("select id from member_car where '{$car_num}' in 
                                          (select t.car_licens from (select car_licens from member_car 
                                          where member_id = {$memberId} limit {$relevance_service['car_impose']}) as t )");
                                if (!empty($car_in_impose)) {
                                    $member_level_discount = $relevance_service;
                                } else {
                                    goto sysdiscount;
                                }
                            } else {
                                $member_level_discount = $relevance_service;
                            }
                        } else {
                            goto sysdiscount;
                        }
                    } else {
                        # 查询会员折扣
                        # 查询当前服务下会员折扣
                        sysdiscount:
                        $member_level_discount = Db::table("service_mediscount")
                            ->field(array("discount"))->where(array("service_id" => $v['id'], 'member_level' => $memberInfo['member_level_id']))->find();
                    }
                    if ($v['is_discount'] == 2 or (empty($member_level_discount)) or (!empty($member_level_discount) and (is_null($member_level_discount['discount']) or $member_level_discount['discount'] == 1))) {
                        $_res_memberdiscount = 1;
                    } else {
                        $_res_memberdiscount = floatval($member_level_discount['discount']);
                    }
                    $list[$k]['res_memberdiscount'] = $_res_memberdiscount;
                    $list[$k]['show_price'] = getsPriceFormat(floatval($v['show_price']) * $_res_memberdiscount);
                }
            }
        } else {
            # 查询服务列表
//            $this->_Redis->delete('addService');
            $list = $this->_Redis->hGetJson('addService', $this->bizId . 'serviceList' . $carLevel);
            // if (empty($list)) {
            $list = Db::table('service s')
                ->field('id,service_title title,service_class_id classId,class_pid,service_keyborder,
                if(' . $carLevel . '>0,
                (select discount from service_car_mediscount where service_id=s.id and car_level_id=' . $carLevel . ' and service_type=2 and biz_id=' . $this->bizId . '),
                (select if(min(discount)=max(discount),(min(discount)),(CONCAT_WS("-",min(discount),max(discount)))) from service_car_mediscount 
                where service_id=s.id and service_type=2 and biz_id=' . $this->bizId . ')) service_price,
                if(' . $carLevel . '>0,
                (select discount from service_car_mediscount where service_id=s.id and car_level_id=' . $carLevel . ' and service_type=2 and biz_id=' . $this->bizId . '),
                (select if(min(discount)=max(discount),(min(discount)),(CONCAT_WS("-",min(discount),max(discount)))) from service_car_mediscount 
                where service_id=s.id and service_type=2 and biz_id=' . $this->bizId . ')) show_price')
                ->where($biz_where)
                ->where(array('audit_status' => 2))
                ->select();

            $this->_Redis->hSetJson('addService', $this->bizId . 'serviceList' . $carLevel, $list);
            //  }
        }
//        if (!empty($memberId)) {
//            # 判断用户状态--会员,注册,未注册
//        }
        if (!empty($classId)) {
            $this->classIdFilter = $classId;
            $list = array_filter($list, function ($v) {
                if ($v['class_pid'] == $this->classIdFilter) {
                    return $v;
                } else {
                    return false;
                }
            });
        }
        if (!empty($title)) {
            $this->serviceTitleFilter = $title;
            $list = array_filter($list, function ($v) {
                if (mb_strpos($v['title'], $this->serviceTitleFilter) !== false or mb_strpos($v['service_keyborder'], $this->serviceTitleFilter) !== false) {
                    return $v;
                } else {
                    return false;
                }
            });
        }
        # 查询是否开启自定义权限
        $open_custom = $bizInfo['open_custom'];
        if ($bizInfo['recommend_status'] == 2) {
            $open_custom = 2;
        }
        # 自定义服务抽成
        $extract_selfservice = $bizInfo['extract_selfservice'];
        # 自定义商品抽成
        $extract_selfpro = $bizInfo['extract_selfpro'];
        return array('status' => true, 'data' => $list, 'open_custom' => $open_custom, 'extract_selfpro' => $extract_selfpro, 'extract_selfservice' => $extract_selfservice);
    }

    /**
     * @return array
     * @content  添加服务工单
     */
    function addServer()
    {
        if (!in_array(1, $this->authority)) {
            return array('status' => false, 'msg' => '无添加服务权限');
        }
        # 接收订单编号
        $orderNumber = input('post.order_number');
        # 接收服务id
        $serviceId = input('post.service_id');
        # 接收服务价格
        $servicePrice = input('post.service_price');
        # 接收车牌号
        $carNum = input('post.car_num');
        # 接收车辆品牌id
        $logoId = input('post.logo_id') ?? 0;
        # 接收车辆系列Id
        $sortId = input('post.sort_id') ?? 0;
        # 接收车辆等级
        $carLevel = input('post.car_level') ?? 0;
        # 车辆公里数
        $mileage = input('post.mileage') ?? 0;
        # 是否需要修改用户车辆信息
        $isModifyCarInfo = input('post.isModifyCarInfo') ?? false;
        # 操作标识 fast=>快速结算
        $mark = input('post.mark') ?? '';
        # 用户id
        $member_id = input('post.member_id') ?? 0;
        $redis = new Redis();
        $status = $redis->lock('addServer' . $carNum);
        if ($status) {
            # 小年判断
            if (!voucherDeadLine()) {
                # 小年期间 ,车辆没绑定银卡以上会员不能使用(优质精洗,微晶洗,深度精洗,快捷精洗,普洗,精洗)
                if (in_array($serviceId, array(253, 12, 11, 10, 9))) {
                    # 查询车辆是否绑定会员
                    /* $memberLevel = Db::table('member_mapping')
                         ->field('member_level_id')
                         ->where("member_id = (select member_id from member_car where car_licens = '" . $carNum . "' and member_id != '' limit 1)")
                         ->find();
                     if (!empty($memberLevel)) {
                         if ($memberLevel['member_level_id'] < 3) {
                             return array('status' => false, 'msg' => '春节期间该服务只为银卡以上会员开放');
                         }
                     } else {
                         return array('status' => false, 'msg' => '春节期间该服务只为银卡以上会员开放');
                     }
                     */
                    $memberLevel = Db::table('member_car mc')
                        ->join('member_mapping mm', 'mm.member_id=mc.member_id', 'left')
                        ->where('mm.member_level_id >= 3 and mc.status != 3')
                        ->where(array('car_licens' => $carNum))
                        ->count();
                    if (empty($memberLevel)) {
                        return array('status' => false, 'msg' => '春节期间该服务只为银卡以上储值用户开放');
                    }
                }
            }
            # 查询服务周期
            $serviceInfo = Db::table('service')->field('repeat_cycle,biz_pid')->where(array('id' => $serviceId))->find();
            $serviceCycle = $serviceInfo['repeat_cycle'];
            if ($serviceCycle > 0) {
                if (!empty($carNum)) {
                    $carLicense = $carNum;
                } else {
                    $carLicense = Db::table('orders')->field('car_liences')->where(array('order_number' => $orderNumber))->value('car_liences');
                }
                # 查询该车辆周期内是否服务过
                $serviceNum = Db::table('order_server')
                    ->where(array('status' => 6, 'custommark' => 1, 'server_id' => $serviceId))
                    ->where("order_number in (select order_number from orders where car_liences = '" . $carLicense . "'
                 and order_create <='" . date('Y-m-d H:i:s') . "' and order_create >= '" . date('Y-m-d H:i:s', strtotime('- ' . $serviceCycle . ' days')) . "')")
                    ->count();
                if ($serviceNum > 0) {
                    return array('status' => false, 'msg' => '此服务已完成，不可重复下单！');
                }
            }
            # 判断平台服务 同一个订单 只能添加同一个平台服务
            if ($serviceInfo['biz_pid'] == 0 and !empty($orderNumber) and $orderNumber != 'undefined') {
                $platformNum = Db::table('order_server')->where(array('order_number' => $orderNumber, 'server_id' => $serviceId, 'custommark' => 1))->count();
                if ($platformNum > 0) {
                    return array('status' => false, 'msg' => '平台服务不可重复下单！');
                }
            }
            # 判断是否存在订单号
            if (empty($orderNumber) or $orderNumber == 'undefined') {
                if (empty($carNum)) {
                    return array('status' => false, 'msg' => '车牌号不能为空');
                }
                # 不存在,添加新订单
                # 创建新订单
                $orderNumber = getOrderNumber($this->bizId);
                $bizInfo = getsBizInfoArray($this->bizId);
                # 查询门店关联员工
                $assignStaff = Db::table('assign_staff')->field('employee_id')->where(array('biz_id' => $this->bizId, 'type' => 1))->find();
                if (empty($assignStaff)) {
                    $assignStaff = 0;
                } else {
                    $assignStaff = $assignStaff['employee_id'];
                }
                # 快速结算的订单直接完成
                if ($mark == 'fast') {
                    $order_status = 6;
                } else {
                    $order_status = 6;
                }
                $_array = array(
                    'member_id' => $member_id,
                    'order_status' => $order_status,
                    'biz_id' => $this->bizId,
                    'order_type' => 2,
                    'biz_pro_id' => $serviceId,
                    'order_create' => date('Y-m-d H:i:s', time()),
                    'order_number' => $orderNumber,
                    'biz_type' => $bizInfo['biz_type'],
                    'car_liences' => $carNum,
                    'car_level' => $carLevel,
                    'car_logo' => $logoId,
                    'car_sort' => $sortId,
                    'car_mileages' => $mileage,
                    'lineup' => getsLineUp($carNum, $this->bizId),
                    'order_price' => $servicePrice,
                    'employee_sal' => $assignStaff,
                    'is_online' => 2  //门店订单 10%抽成
                );
                Db::table('orders')->insert($_array);
            } else {
                # 修改
                Db::table('orders')->where(array('order_number' => $orderNumber))->update(array('biz_pro_id' => $serviceId));
            }
            $orderServerId = 0;
            if ($serviceId != 0 and $serviceId != '' and !empty($serviceId)) {
                # 添加服务工单
                # 快速结算的订单直接完成
                if ($mark == 'fast') {
                    $order_server_status = 4;
                } else {
                    $order_server_status = 1;
                }
                $orderServerId = Db::table("order_server")
                    ->insertGetId(array("order_number" => $orderNumber,
                        "server_id" => $serviceId,
                        "status" => $order_server_status,
                        "price" => $servicePrice,
                        "original_price" => $servicePrice,
                    ));
            }
            # 修改用户车辆信息
            if (!empty($isModifyCarInfo) and $isModifyCarInfo != "undefined" and $isModifyCarInfo != "false") {
                Db::table('member_car')
                    ->where(array('car_licens' => $carNum))
                    ->update(array('car_type_id' => $sortId, 'car_mileage' => $mileage));
            }
            $payment = new Payment();
            $payment->WriteOrderPrice(true, $orderNumber);
            $redis->unlock('addServer' . $carNum);
            return array('status' => true, 'order_number' => $orderNumber, 'order_server_id' => $orderServerId);
        }
    }

    /**
     * @return array
     * @content 添加自定义服务
     */
    function addCustomOrder()
    {
        if (!in_array(1, $this->authority)) {
            return array('status' => false, 'msg' => '无添加服务权限');
        }
        # 查询门店时候开通添加自定义服务权限
        $bizInfo = getsBizInfoArray($this->bizId, true);
        if ($bizInfo['open_custom'] == 2) {
            return array('status' => false, 'msg' => '无添加自定义服务权限');
        }
        # 接收类型(商品还是服务)(1:服务  2:商品)
        $type = input('post.type');
//        # 接收分类id
//        $classId = input('post.class_id');
        # 接收自定义名称
        $title = input('post.title');
        # 接收进价
        $purchase_price = input('post.purchase_price');
        # 接收卖价
        $price = input('post.price');
        # 接收订单编号
        $orderNumber = input('post.order_number');
        # 接收车牌号
        $carNum = input('post.car_num');
        # 接收车辆品牌
        $logoId = input('post.logo_id');
        # 接收车辆系列id
        $sortId = input('post.sort_id');
        # 接收车辆等级
        $carLevel = input('post.car_level');
        # 数量
        $num = input('post.num') ?? 1;
        # 车辆公里数
        $mileage = input('post.mileage') ?? 0;
        # 是否修改车辆信息
        $isModifyCarInfo = input('post.isModifyCarInfo');
        # 开启折扣/关闭折扣  1->开启折扣  0->关闭折扣
        $price_lock = input('post.price_lock') ?? 0;
        # 关联记录类型
        $log_type = input('post.log_type');
        # 有效期
        $log_validity = input('post.log_validity');
        # 公里数
        $log_kilometers = input('post.log_kilometers');
        # 名称
        $log_title = input('post.log_title');
        # 质保范围
        $log_scope = input('post.log_scope');
        # 判断是否存在订单
        if (empty($orderNumber) or $orderNumber == 'undefined') {
            # 添加新订单
            $orderNumber = getOrderNumber($this->bizId);
            # 查询门店关联员工
            $assignStaff = Db::table('assign_staff')->field('employee_id')->where(array('biz_id' => $this->bizId, 'type' => 1))->value('employee_id');
            if (empty($assignStaff)) {
                $assignStaff = 0;
            }
            $_array = array(
                'order_status' => 6,
                'biz_id' => $this->bizId,
                'order_type' => 2,
                'biz_pro_id' => 0,
                'order_create' => date('Y-m-d H:i:s', time()),
                'order_number' => $orderNumber,
                'biz_type' => $bizInfo['biz_type'],
                'car_liences' => $carNum,
                'car_level' => $carLevel,
                'car_logo' => $logoId,
                'car_sort' => $sortId,
                'lineup' => getsLineUp($carNum, $this->bizId),
                'order_price' => $price,
                'car_mileages' => $mileage,
                'employee_sal' => $assignStaff
            );
            Db::table('orders')->insert($_array);
            # 添加订单redis
        } else {
            # 修改订单总价
            Db::table('orders')->where(array('order_number' => $orderNumber))->setInc('order_price', $price);
        }
        # 根据类型添加工单
        if ($type == 2) {
            # 服务工单(先添加自定义服务)
            $serverId = Db::table("customserver")->insertGetId(array(
                "custom_order_number" => $orderNumber,
                "custom_server_title" => $title,
                "custom_server_price" => $price,
//                "custom_server_class" => $classId
                "log_type" => $log_type,
                "log_validity" => $log_validity,
                "log_kilometers" => $log_kilometers,
                "log_title" => $log_title,
                "log_scope" => $log_scope,
            ));
            $order_server_id = Db::table("order_server")->insertGetId(array(
                "order_number" => $orderNumber,
                "server_id" => $serverId,
                "status" => 1,
                'price_lock' => $price_lock,
                "price" => $price,
                "original_price" => $price,
                "type" => 2,
                "custommark" => 2,
            ));
            # 添加服务工单redis
        } else {
            # 商品工单(先添加自定义商品)
            $proId = Db::table("custom_biz")->insertGetId(array(
                "custom_order_number" => $orderNumber,
                "custom_biz_title" => $title,
                "custom_biz_price" => $price,
//                "custom_biz_class" => $classId,
                "custom_biz_prime" => $purchase_price,
                "log_type" => $log_type,
                "log_validity" => $log_validity,
                "log_kilometers" => $log_kilometers,
                "log_title" => $log_title,
                "log_scope" => $log_scope,
            ));
            $order_server_id = Db::table("order_biz")->insertGetId(array(
                "biz_pro_id" => $proId,
                "pro_price" => floatval($price) * $num,
                "original_pro_price" => floatval($price) * $num,
                "status" => 1,
                'price_lock' => $price_lock,
                "order_number" => $orderNumber,
                "custommark" => 2,
                "bizpro_number" => $num
            ));
            # 条件商品工单redis
        }
        # 修改用户车辆信息
        if (!empty($isModifyCarInfo) and $isModifyCarInfo != "undefined" and $isModifyCarInfo != "false") {
            Db::table('member_car')
                ->where(array('car_licens' => $carNum))
                ->update(array('car_type_id' => $sortId, 'car_mileage' => $mileage));
        }
        $payment = new Payment();
        $payment->WriteOrderPrice(true, $orderNumber);
        return array('status' => true, 'data' => array('order_number' => $orderNumber, 'order_server_id' => $order_server_id));
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 进入等待
     */
    function enterWaiting()
    {
        if (!in_array(1, $this->authority)) {
            return array('status' => false, 'msg' => '无进入等待权限');
        }
        #接收订单编号
        $order_number = input('post.order_number');
        if (!empty($order_number)) {
            # 查询订单信息,判断是不是预约订单
            $orderInfo = Db::table('orders')->field('id,order_type')->where(array('order_number' => $order_number))->find();
            if ($orderInfo['order_type'] == 3) {
                Db::table('orders')->where(array('order_number' => $order_number))->update(array('order_status' => 6));
            } else {
                return array('status' => false, 'msg' => '订单已存在');
            }
        }
        # 接收车牌号
        $carNum = input('post.car_num');
        if (empty($carNum)) {
            return array('status' => false, 'msg' => '车牌号不能为空');
        }
        # 接收车辆品牌
        $logoId = input('post.logo_id');
        # 接收车辆系列id
        $sortId = input('post.sort_id');
        # 接收车辆等级
        $carLevel = input('post.car_level');
        # 车辆公里数
        $mileage = input('post.mileage') ?? 0;
        # 添加新订单
        $orderNumber = getOrderNumber($this->bizId);
        $bizInfo = getsBizInfoArray($this->bizId);
        # 查询门店关联员工
        $assignStaff = Db::table('assign_staff')->field('employee_id')->where(array('biz_id' => $this->bizId, 'type' => 1))->value('employee_id');
        if (empty($assignStaff)) {
            $assignStaff = 0;
        }
        $_array = array(
            'order_status' => 6,
            'biz_id' => $this->bizId,
            'order_type' => 2,
            'biz_pro_id' => 0,
            'order_create' => date('Y-m-d H:i:s'),
            'order_number' => $orderNumber,
            'biz_type' => $bizInfo['biz_type'],
            'car_liences' => $carNum,
            'car_level' => $carLevel,
            'car_logo' => $logoId,
            'car_sort' => $sortId,
            'lineup' => getsLineUp($carNum, $this->bizId),
            'car_mileages' => $mileage,
            'employee_sal' => $assignStaff
        );
        Db::table('orders')->insert($_array);
        return array('status' => true);
    }

    /**
     * @return array
     * @content  预约订单点击到店，修改订单状态
     */
    function memberCome()
    {
        # 点击到店
        $order_id = input('post.order_id');
        if (empty($order_id)) {
            return array('status' => false, 'msg' => '系统错误，订单编号为空');
        }
        # 修改订单状态,到店时间
        Db::table('orders')->where(array('id' => $order_id))
            ->update(array('order_status' => 6, 'arrive_time' => date('Y-m-d H:i:s')));
        return array('status' => true, 'msg' => '到店进入等待');
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 开始服务(包含选择工位和直接开始的)
     */
    function startService()
    {
        if (!in_array(1, $this->authority)) {
            return array('status' => false, 'msg' => '无开始服务权限');
        }
        # 接收选择的工位id(可以为空,为空时是直接开始)
        $station_id = input("post.station_id");
        # 接收服务工单id
        $orderServer_id = input("post.orderServer_id");
        if (!empty($orderServer_id)) {
            # 开始时间
            $start = date("Y-m-d H:i:s");
            if (!empty($station_id)) {
                # 选择工位了
                $this->_Redis->hSet('stationlist' . $this->bizId, $station_id, $orderServer_id);
                # 查询服务工单
                $_array = array();
                $order_server = Db::table("order_server")
                    ->field(array("server_id", "custommark", "employee_id", "order_timer"))
                    ->where(array("id" => $orderServer_id))
                    ->find();
                if (empty($order_server['employee_id'])) {
                    # 获取工位对应的团队人员
                    $employee = getsEmployeeId($station_id);
                    if (!empty($employee)) {
                        $employeeId = explode(',', $employee);
                        # 查询今天未出勤的员工
                        $log_attendance = Db::table('log_attendance')->field('employee_id')
                            ->where("date(create_time) = '" . date("Y-m-d") . "'")
                            ->select();
                        if (!empty($log_attendance)) {
                            $attendance = array_column($log_attendance, 'employee_id');
                            $employeeId = array_diff($employeeId, $attendance);
                        }
                        $_array['employee_id'] = implode(',', $employeeId);
                    }
                }
                if ($order_server['custommark'] == 1) {
                    # 查询工位计时状态 服务计时状态
                    $station_timer = Db::table("station")->field("timer")->where(array("id" => $station_id))->find();
                    if ($station_timer['timer'] == 1) {
                        $server_timer = Db::table('service_biz sb')
                            ->field('sb.timer,s.service_time')
                            ->join('service s', 's.id=' . $order_server['server_id'], 'left')
                            ->where(array('sb.service_id' => $order_server['server_id'], 'biz_id' => $this->bizId))
                            ->find();
                        if ($server_timer['timer'] == 1) {
                            $end = date("Y-m-d H:i:s", strtotime("+" . $server_timer['service_time'] . " minutes", strtotime($start)));
                            $_array['order_endtime'] = $end;
                            $_array['order_timer'] = 1;
                        }
                    }
                }
                $_array['order_starttime'] = $start;
                $_array['status'] = 2;
                $_array['station_id'] = $station_id;
                # 将工单变为服务中订单
                Db::table("order_server")->where(array("id" => $orderServer_id))->update($_array);

            } else {
                # 没有选择工位直接开始的(状态修改为已完成)
                Db::table("order_server")->where(array("id" => $orderServer_id))->update(array('status' => 4, 'order_starttime' => $start));
            }

            # 修改预约订单状态
            Db::table('orders')->where("order_number = (select order_number from order_server where id = " . $orderServer_id . ") and order_type = 3")->update(array('order_status' => 6));
            return array('status' => true);
        } else {
            return array('status' => false, 'msg' => '服务工单id为空');
        }
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 完成服务
     */
    function completeService()
    {
        if (!in_array(2, $this->authority)) {
            return array('status' => false, 'msg' => '无完成服务权限');
        }
        # 工单id
        $orderServer_id = input("post.orderServer_id");
        # 类型  1服务 2商品
        $orderType = input('post.orderType');
        if ($orderType == 1) {
            $timer = Db::table('order_server')
                ->field('order_timer,order_endtime,order_number,employee_id,order_starttime,order_end,station_id ')
                ->where(array('id' => $orderServer_id))
                ->find();
            if (!empty($timer['station_id'])) {
                # 清空工位redis
                $redis = new Redis();
                $redis->hSet('stationlist' . $this->bizId, $timer['station_id'], '');
            }
            $date = date("Y-m-d H:i:s");
            if ($timer['order_timer'] == 1) {
                if ($date > $timer['order_endtime']) {
                    $_array['order_timeout'] = 0;
                    if (!empty($timer['employee_id'])) {
                        $employee = explode(",", $timer['employee_id']);
                        $log_order_timeout = array();
                        foreach ($employee as $v) {
                            array_push($log_order_timeout, array(
                                "order_server_id" => $orderServer_id,
                                "order_number" => $timer['order_number'],
                                'employee_id' => $v,
                                "order_starttime" => $timer['order_starttime'],
                                "order_endtime" => $timer['order_endtime'],
                                "order_over" => $date));
                        }
                        Db::table("log_order_timeout")->insertAll($log_order_timeout);
                    }
                }
            }
            if (empty($timer['order_end'])) {
                $_array['order_end'] = $date;
            }
            $_array['status'] = 4;
            Db::table("order_server")->where(array("id" => $orderServer_id))->update($_array);
        } else {
            # 工单
            # 查询工位
            $station_id = Db::table('order_biz')->field('station_id')->where(array('id' => $orderServer_id))->value('station_id');
            if (!empty($station_id)) {
                $this->_Redis->hSet('stationlist' . $this->bizId, $station_id, '');
            }
            Db::table("order_biz")->where(array("id" => $orderServer_id))->update(array('status' => 2));
        }
        return array('status' => true);
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * @content 停止服务
     */
    function outOfService()
    {
        if (!in_array(2, $this->authority)) {
            return array('status' => false, 'msg' => '无停止服务权限');
        }
        # 工单id
        $orderServer_id = input("post.orderServer_id");
        # 类型 1服务 2商品
        $orderType = input('post.orderType');
        if (empty($orderServer_id)) {
            return array('status' => false, 'nsg' => '参数信息为空');
        }
        if ($orderType == 1) {
            # 查询工单所在的工位
            $station_id = Db::table('order_server')->field('station_id')->where(array('id' => $orderServer_id))->value('station_id');
            if (!empty($station_id)) {
                # 清空工位redis
                $this->_Redis->hSet('stationlist' . $this->bizId, $station_id, '');
            }
            Db::table("order_server")->where(array("id" => $orderServer_id))->update(array('status' => 1, "order_timer" => 0));
        } else {
            # 查询工位
            $station_id = Db::table('order_biz')->field('station_id')->where(array('id' => $orderServer_id))->value('station_id');
            if (!empty($station_id)) {
                $this->_Redis->hSet('stationlist' . $this->bizId, $station_id, '');
            }
            Db::table("order_biz")->where(array("id" => $orderServer_id))->update(array('status' => 1));
        }
        return array('status' => true, 'msg' => '成功停止');
    }

    /**
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 删除工单
     */
    function delWorkOrder()
    {
        if (!in_array(2, $this->authority)) {
            return array('status' => false, 'msg' => '无删除服务权限');
        }
        # 订单编号
        $order_number = input('post.order_number');
        # 工单id
        $workOrder_id = input('post.work_order_id');
        # 工单类型(1服务工单  2商品工单)
        $workOrder_type = input('post.work_order_type');
        if (empty($order_number) or empty($workOrder_id) or empty($workOrder_type)) {
            return array('status' => false, 'msg' => '参数信息为空');
        }
        # 删除工单
        if ($workOrder_type == 1) {
            # 查询是否是自定义
            $info = Db::table('order_server')->field('server_id,custommark')->where(array('id' => $workOrder_id))->find();
            if ($info['custommark'] == 2) {
                Db::table('customserver')->where(array('id' => $info['server_id']))->delete();
            }
            Db::table('order_server')->where(array('id' => $workOrder_id))->delete();
        } else {
            # 查询是否是自定义
            $info = Db::table('order_biz')->field('biz_pro_id,custommark')->where(array('id' => $workOrder_id))->find();
            if ($info['custommark'] == 2) {
                Db::table('custom_biz')->where(array('id' => $info['biz_pro_id']))->delete();
            }
            Db::table('order_biz')->where(array('id' => $workOrder_id))->delete();
        }
        $payment = new Payment();
        $payment->writeOrderPrice(true, $order_number);
        return array('status' => true, 'msg' => '删除成功');
    }

    /**
     * @return array
     * @throws \Yansongda\Pay\Exceptions\GatewayException
     * @throws \Yansongda\Pay\Exceptions\InvalidArgumentException
     * @throws \Yansongda\Pay\Exceptions\InvalidConfigException
     * @throws \Yansongda\Pay\Exceptions\InvalidSignException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 取消订单
     */
    function cancellationOrder()
    {
        if (!in_array(2, $this->authority)) {
            return array('status' => false, 'msg' => '无取消订单权限');
        }
        $res = array();
        # 订单编号
        $order_number = input('post.order_number');
        if (empty($order_number)) {
            return array('status' => false, 'msg' => '订单编号为空');
        }
        # 查询订单信息
        $orderType = Db::table('orders')->field('id,order_type,member_id,member_car_id,order_status')->where(array('order_number' => $order_number))->find();
        if ($orderType['order_status'] == 5) {
            return array('status' => false, 'msg' => '该订单已完成,无法取消');
        }
        if ($orderType['order_type'] == 3) {
            # 预约订单,退款50%
            $res = TimeTask::notInStore($order_number, 1);
        } else {
            # 删除订单
//            Db::table('orders')->where(array('order_number' => $order_number))->delete();
//            # 删除工单
//            Db::table('order_server')->where(array('order_number' => $order_number))->delete();
//            Db::table('order_biz')->where(array('order_number' => $order_number))->delete();
            Db::table('orders')->where(array('order_number' => $order_number))->update(array('order_status'=>4));
            # 删除工单
            Db::table('order_server')->where(array('order_number' => $order_number))->update(array('status'=>9));
            # 删除卡券使用
            Db::table('log_cashcoupon')->where(array('order_number' => $order_number,'member_id'=>$orderType['member_id']))->delete();
            Db::table("member_voucher")
                ->where(array("order_id" => $orderType['id']))
                ->update(array("status" => 1));
        }
        # 判断是不是新用户
        $newMember = $this->_Redis->hGet('cooperation_newMember', $order_number);
        if (!empty($newMember)) {
            # 删除用户和车
            Db::table('member')->where(array('id' => $orderType['member_id']))->delete();
            Db::table('member_mapping')->where(array('id' => $newMember))->delete();
            Db::table('member_car')->where(array('id' => $orderType['member_car_id']))->delete();
            $this->_Redis->hDel('cooperation_newMember', $order_number);
        }
        return array('status' => true, 'msg' => '取消成功', 'res' => $res);
    }

    /**
     * @access public
     * @content 商品结算
     */
    function goodsSettlement()
    {
        if (!in_array(2, $this->authority)) {
            return array('status' => false, 'msg' => '无订单结算权限');
        }
        # 接收工单id
        $orderId = Input('post.order_server_id');
        # 接收工位id
        $station_id = Input('post.station_id');
        if (!empty($orderId)) {
            if (!empty($station_id)) {
                $this->_Redis->hSet('stationlist' . $this->bizId, $station_id, 'orderBiz' . $orderId);
                $order_server = Db::table("order_biz")
                    ->field("employee_id")
                    ->where(array("id" => $orderId))
                    ->find();
                # 工位结算
                $_data = array('status' => 4, 'station_id' => $station_id);
                if (empty($order_server['employee_id'])) {
                    # 获取工位对应的团队人员
                    $employee = getsEmployeeId($station_id);
                    if (!empty($employee)) {
                        $employeeId = explode(',', $employee);
                        # 查询今天未出勤的员工
                        $log_attendance = Db::table('log_attendance')->field('employee_id')
                            ->where("date(create_time) = '" . date("Y-m-d") . "'")
                            ->select();
                        if (!empty($log_attendance)) {
                            $attendance = array_column($log_attendance, 'employee_id');
                            $employeeId = array_diff($employeeId, $attendance);
                        }
                        $_data['employee_id'] = implode(',', $employeeId);
                    }
                }
            } else {
                # 直接结算
                $_data = array('status' => 2);
            }
            Db::table("order_biz")->where(array("id" => $orderId))->update($_data);
            return array('status' => true);
        } else {
            return array('status' => false, 'msg' => '工单id为空');
        }
    }
}
